package com.ruoyi.web.controller.api.city;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.city.*;
import com.ruoyi.system.service.city.*;
import com.ruoyi.web.core.config.SearchRedisHelper;
import com.ruoyi.web.core.utils.OSSUtil;
import com.ruoyi.web.core.utils.UserRecentSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.util.*;

import static com.ruoyi.common.utils.file.FileUploadUtils.assertAllowed;

/**
 * 我的Controller
 *
 * @author cjq
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/api/city/mine")
public class CityMineController extends BaseController {



    @Autowired
    private ICityCollectService cityCollectService;

    @Autowired
    private ICityProjectService cityProjectService;

    @Autowired
    private ICityUserService cityUserService;

    @Autowired
    private ICityShareService cityShareService;

    @Resource
    private SearchRedisHelper searchRedisHelper;


    @Resource
    private RedisTemplate redisTemplate;






    /**
     * 删除redis
     * @param key
     * @return
     */
    @GetMapping("/removeRedis")
    public Boolean removeRedis(String key){

        return redisTemplate.delete(key);
    }



    /**
     * 最近搜索列表
     * @return
     */
    @GetMapping("/listRecentSearch")
    public Set<UserRecentSearch> listRecentSearch(UserRecentSearch userRecentSearch) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userRecentSearch.getUserId());
        CityUser cityUserDB = cityUserService.getOne(queryWrapper);
        if (cityUserDB!=null){
            userRecentSearch.setUserId(cityUserDB.getUserId());
            userRecentSearch.setUnionId(cityUserDB.getUserId());
        }
        return searchRedisHelper.listRecentSearch(userRecentSearch);
    }


    /**
     * 查询分享项目列表
     */
    @GetMapping("/shareList")
    public TableDataInfo shareList(CityShare cityShare)
    {
        startPage();
        List<CityProject> list = cityProjectService.selectShareCityProjectList(cityShare.getUserId());
        return getDataTable(list);
    }

    /**
     * 新增分享项目
     */
    @PostMapping("/addCityShare")
    public void add(@RequestBody CityShare cityShare)
    {
        List<CityShare> cityCollectList = cityShareService.selectCityShareList(cityShare);
        cityShare.setShareId(IdUtil.getSnowflakeNextId());
        if (cityCollectList.isEmpty()) {
            CityProject cityProject = cityProjectService.selectCityProjectByProjectId(cityShare.getProjectId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", cityShare.getUserId());
            CityUser cityUser = cityUserService.getOne(queryWrapper);
            if (cityProject != null && cityUser != null) {
                cityShare.setNickName(cityUser.getNickName());
                cityShare.setPhonenumber(cityUser.getPhonenumber());
                cityShare.setProjectName(cityProject.getProjectName());
                cityShare.setProjectTitle(cityProject.getProjectTitle());
                cityShare.setUserType(0);
                cityShare.setProjectId(cityProject.getProjectId());
                cityShare.setUserId(cityUser.getUserId());
                cityProject.setProjectShare(cityProject.getProjectShare()+1);
                cityProjectService.updateCityProject(cityProject);
                cityShareService.insertCityShare(cityShare);
            }
        }
    }

    /**
     * 删除分享项目
     */
    @DeleteMapping("/{shareIds}")
    public AjaxResult remove(@PathVariable Long[] shareIds)
    {
        return toAjax(cityShareService.deleteCityShareByShareIds(shareIds));
    }


    /**
     * 查询收藏的项目分页列表
     */
    @GetMapping("/collectList")
    public TableDataInfo cityCollectList(CityCollect cityCollect) {
        startPage();
        List<CityProject> list = cityProjectService.selectCollectCityProjectList(cityCollect.getUserId());
        return getDataTable(list);
    }





    /**
     * 新增收藏
     */
    @PostMapping("/addCollect")
    public AjaxResult addCityCollect(@RequestBody CityCollect cityCollect) {
        cityCollect.setCollectId(IdUtil.getSnowflakeNextId());

        List<CityCollect> cityCollectList = cityCollectService.selectCityCollectList(cityCollect);
        if (!cityCollectList.isEmpty()) {
            return toAjax(0);
        }
        CityProject cityProject = cityProjectService.selectCityProjectByProjectId(cityCollect.getProjectId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", cityCollect.getUserId());
        CityUser cityUser = cityUserService.getOne(queryWrapper);
        if (cityProject != null && cityUser != null) {
            cityCollect.setNickName(cityUser.getNickName());
            cityCollect.setPhonenumber(cityUser.getPhonenumber());
            cityCollect.setProjectName(cityProject.getProjectName());
            cityCollect.setProjectTitle(cityProject.getProjectTitle());
            cityCollect.setUserType(0);
            cityCollect.setProjectId(cityProject.getProjectId());
            cityCollect.setUserId(cityUser.getUserId());
            cityProject.setProjectCollect(cityProject.getProjectCollect()+1);
            cityProjectService.updateCityProject(cityProject);
        }
        return toAjax(cityCollectService.insertCityCollect(cityCollect));
    }

    /**
     * 新增浏览量（点击量）
     */
    @GetMapping("/addProjectHits")
    public AjaxResult addProjectHits(@RequestParam("projectId") Long projectId) {

        CityProject cityProject = cityProjectService.selectCityProjectByProjectId(projectId);
        if (cityProject != null) {
            System.out.println("cityProject"+cityProject);
            Integer count=cityProject.getProjectHits();
            cityProject.setProjectHits(count+1);
        }
        return toAjax(cityProjectService.updateCityProject(cityProject));
    }


    /**
     * 删除收藏
     */

    @DeleteMapping("/delCollect/{projectId}")
    public AjaxResult remove(@PathVariable Long projectId) {
        return toAjax(cityCollectService.deleteCityCollectByCollectId(projectId));

    }


}
